package com.deram.ctin.serv.enter.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.enter.entity.BaseUpdateInfo;
import com.deram.ctin.serv.enter.entity.EnterChangeLog;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.enter.filter.EnterChangeLogFilter;
import com.deram.ctin.serv.enter.mapper.EnterChangeLogMapper;
import com.deram.ctin.serv.enter.service.EnterChangeLogService;
import com.deram.ctin.serv.enter.service.EnterInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: jtt
 * @Description:
 * @Date: 2017/9/13 11:20
 * @version: V1.0
 */
@Service
public class EnterChangeLogServiceImpl implements EnterChangeLogService {

    @Autowired
    EnterChangeLogMapper enterChangeLogMapper;

    @Autowired
    EnterInfoService enterInfoService;

    @Override
    public EnterChangeLog find(Long id) {
        ValidateUtils.isId(id, "企业变更ID");
        EnterChangeLog result = enterChangeLogMapper.selectById(id);
        BusinessUtils.notNull(result, "企业变更信息不存在");
        return result;
    }

    @Override
    public PageResult<EnterChangeLog> search(EnterChangeLogFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo, pageSize);
        EntityWrapper<EnterChangeLog> wrapper = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(enterChangeLogMapper.selectPage(pagination,wrapper),pagination);
    }

    private EntityWrapper<EnterChangeLog> createEntityWrapper(EnterChangeLogFilter filter) {
        EnterChangeLog entity = new EnterChangeLog();
        entity.setEnterId(filter.getEnterId());
        EntityWrapper<EnterChangeLog> wrapper = new EntityWrapper<>(entity);
        return wrapper;
    }

    @Override
    public List<EnterChangeLog> list(EnterChangeLogFilter filter) {
        ValidateUtils.isId(filter.getEnterId(), "企业ID");
        EntityWrapper<EnterChangeLog> ew = createEntityWrapper(filter);
        List<EnterChangeLog> result = enterChangeLogMapper.selectList(ew);
        return result;
    }

    @Override
    public EnterChangeLog save(EnterChangeLog param) {
        ValidateUtils.isId(param.getEnterId(), "企业ID");
        if (null != param.getId()) {
            EnterChangeLog finder = find(param.getId());
            enterChangeLogMapper.updateById(param);
        } else {
            enterChangeLogMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        enterChangeLogMapper.deleteById(find(id));
    }

    @Override
    public void deleteByEnterId(Long id) {
        EnterChangeLogFilter filter = new EnterChangeLogFilter();
        filter.setEnterId(id);
        List<EnterChangeLog> lists = list(filter);
        if (null != lists && !lists.isEmpty()) {
            List<Long> ids = ArrayHandleUtils.readAttrList(lists, "id");
            enterChangeLogMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public void saveList(BaseUpdateInfo<EnterChangeLog> updateInfo) {
        Long enterId = updateInfo.getEnterId();
        EnterInfo enterInfo = enterInfoService.find(enterId);
        if (null == enterInfo) {
            throw new BusinessException(ErrorConsts.VALIDATE, "工商信息未找到");
        }
        deleteByEnterId(enterId);
        List<EnterChangeLog> lists = updateInfo.getList();
        for (EnterChangeLog item : lists) {
            item.setId(null);
            item.setEnterId(enterId);
        }
        enterChangeLogMapper.batchInsert(lists);
    }
}
